package com.iotechn.unimall.admin.api.leaseDetails;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.LeaseCategoryDO;
import com.iotechn.unimall.data.domain.LeaseDO;
import com.iotechn.unimall.data.domain.LeaseDetailsDO;
import com.iotechn.unimall.data.mapper.LeaseDetailsMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Service
public class AdminLeaseDetailsServiceImpl implements AdminLeaseDetailsService {
    @Autowired
    private LeaseDetailsMapper leaseDetailsMapper;

    @Override
    public Page<LeaseDetailsDO> list(Integer page, Integer limit, String landNo, Integer leaseId, Long adminId) throws ServiceException {
        List<LeaseDetailsDO> LeaseDetailsDO = leaseDetailsMapper.selectByList(landNo, leaseId, (page - 1) * limit, limit);
        Integer count = leaseDetailsMapper.selectCount(new EntityWrapper<>());
        return new Page<>(LeaseDetailsDO, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeaseDetailsDO create(LeaseDetailsDO leaseDetailsDO, Long adminId) throws ServiceException {
        Wrapper<LeaseDetailsDO> wrapper = new EntityWrapper<LeaseDetailsDO>()
                .eq("land_no", leaseDetailsDO.getLandNo())
                .eq("lease_id", leaseDetailsDO.getLeaseId());
        if (leaseDetailsMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_LAND_CANNOT_REPEATED);
        if (leaseDetailsMapper.insert(leaseDetailsDO) > 0) {
            leaseDetailsDO.setId(leaseDetailsDO.getId());
            return leaseDetailsDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(LeaseDetailsDO leaseDetailsDO, Long adminId) throws ServiceException {
        Wrapper<LeaseDetailsDO> wrapper = new EntityWrapper<LeaseDetailsDO>()
                .eq("land_no", leaseDetailsDO.getLandNo())
                .eq("lease_id", leaseDetailsDO.getLeaseId())
                .ne("id", leaseDetailsDO.getId());
        if (leaseDetailsMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_LAND_CANNOT_REPEATED);
        Long id = leaseDetailsDO.getId();
        if (id == null) return "fail";
        if (leaseDetailsMapper.updateById(leaseDetailsDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (leaseDetailsMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public LeaseDetailsDO detail(Long id, Long adminId) throws ServiceException {
        return leaseDetailsMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        if (leaseDetailsMapper.delete(new EntityWrapper<LeaseDetailsDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }
}
